package com.xinchao.api;

import com.xinchao.constant.ResponseConstant;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.sys.JsonResponse;
import com.xinchao.service.AdvertisementMonitorBaseDataSerivce;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;

/**
 * @author dxy
 * @date 2018/11/12 14:37
 */
@Controller
@RequestMapping(value = "/baseData")
public class AdvertisementMonitorBaseDataController {
	private static Logger logger = LoggerFactory.getLogger(AdvertisementMonitorBaseDataController.class);

	@Autowired
	private AdvertisementMonitorBaseDataSerivce advertisementMonitorBaseDataSerivce;

	/**
	 * 获取基础数据-监播-曝光次数列表
	 */
	@ResponseBody
	@RequestMapping(value = "/listExposuresNumber")
	public Object listExposuresNumber(String advertisementName, Integer timeType) {
		//广告名称
		if (StringUtils.isBlank(advertisementName)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告名称不能为空");
		}
		//时间类型
		if (timeType == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "时间类型不能为空");
		}
		try {
			return advertisementMonitorBaseDataSerivce.listExposuresNumberVO(advertisementName, timeType);
		} catch (ServiceException e) {
			logger.error("AdvertisementMonitorBaseDataController listExposuresNumber 获取基础数据-监播-曝光次数列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, e.getMessage());
		} catch (ParseException e) {
			logger.error("AdvertisementMonitorBaseDataController listExposuresNumber 获取基础数据-监播-曝光次数列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, "系统错误，请联系管理员");
		}
	}

	/**
	 * 获取基础数据-监播-触达人次、观看人次列表
	 */
	@ResponseBody
	@RequestMapping(value = "/listTouchWatchSum")
	public Object listTouchWatchSum(String advertisementName, Integer timeType) {
		//广告名称
		if (StringUtils.isBlank(advertisementName)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告名称不能为空");
		}
		//时间类型
		if (timeType == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "时间类型不能为空");
		}
		try {
			return advertisementMonitorBaseDataSerivce.listTouchWatchSumVO(advertisementName, timeType);
		} catch (ServiceException e) {
			logger.error("AdvertisementMonitorBaseDataController listTouchWatchSum 获取基础数据-监播-触达人次、观看人次列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, e.getMessage());
		} catch (ParseException e) {
			logger.error("AdvertisementMonitorBaseDataController listTouchWatchSum 获取基础数据-监播-触达人次、观看人次列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, "系统错误，请联系管理员");
		}
	}

	/**
	 * 获取基础数据-监播-平均观看时长列表
	 */
	@ResponseBody
	@RequestMapping(value = "/listAvgWatchSum")
	public Object listAvgWatchSum(String advertisementName, Integer timeType) {
		//广告名称
		if (StringUtils.isBlank(advertisementName)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告名称不能为空");
		}
		//时间类型
		if (timeType == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "时间类型不能为空");
		}
		try {
			return advertisementMonitorBaseDataSerivce.listAvgWatchSumVO(advertisementName, timeType);
		} catch (ServiceException e) {
			logger.error("AdvertisementMonitorBaseDataController listAvgWatchSum 获取基础数据-监播-平均观看时长列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, e.getMessage());
		} catch (ParseException e) {
			logger.error("AdvertisementMonitorBaseDataController listAvgWatchSum 获取基础数据-监播-平均观看时长列表失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, "系统错误，请联系管理员");
		}
	}
}
